﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using nwrole.toolbox.mailservice.EmailSerialization;
using System.Net.Mail;

namespace nwrole.toolbox.mailservice
{
    public class MailServiceServer
    {
        private string storageAccount;
        private string largeMessageBlobContainerName;
        private string queueName;

        public MailServiceServer(string storageAccount, string largeMessageBlobContainerName, string queueName)
        {
            this.storageAccount = storageAccount;
            this.largeMessageBlobContainerName = largeMessageBlobContainerName;
            this.queueName = queueName;
        }

        public void ProcessMessages()
        {
            AzureCloudQueueClient client = new AzureCloudQueueClient(storageAccount, largeMessageBlobContainerName, queueName);

            while(true)
            {
                SerializeableMailMessage serMsg;

                if(client.DequeueMessage(out serMsg) == null)
                    break;

                MailMessage msg = serMsg.GetMailMessage();

                SmtpClient smtp = new SmtpClient();
                smtp.Send(msg);
            }
        }
    }
}
